import { loginApi } from '@/api/login'
import type { Action, LoginData, Token } from '@/types/login'
import { Dispatch } from 'redux'
import { RootThunkAction } from '@/store'
// 登录
export const login = (tokenInfo: Token): Action => ({
  type: 'LOGIN',
  payload: tokenInfo,
})
// 异步登录
export const loginAsync = (postData: LoginData): RootThunkAction => {
  return async (dispatch: Dispatch) => {
    // 发网络请求获取登录数据
    const { data } = await loginApi(postData)
    dispatch(login(data) as Action)
  }
}

// 退出登录
export const logout = (): Action => ({
  type: 'LOGOUT',
  payload: { token: '', refresh_token: '' }
})
